#include "tools/Settings/SettingsParser.h"
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <getopt.h>
#include <spdlog/spdlog.h>

void printUsage(const std::string& programName) {
    std::cerr << "Usage: " << programName << " [options]\n"
              << "Options:\n"
              << "  -f, --file <path>   Specify the settings file path (default: settings)\n"
              << "  -h, --help          Show this help message\n";
}

int main(int argc, char* argv[]) {
    // 检查参数数量是否太少
    if (argc < 2) {
        printUsage(argv[0]);
        return EXIT_FAILURE;
    }

    std::string filePath = "settings"; // 默认配置文件路径

    int opt;
    const char* const short_opts = "f:h";
    const struct option long_opts[] = {
        {"file", required_argument, nullptr, 'f'},
        {"help", no_argument, nullptr, 'h'},
        {nullptr, 0, nullptr, 0}
    };

    while ((opt = getopt_long(argc, argv, short_opts, long_opts, nullptr)) != -1) {
        switch (opt) {
            case 'f':
                filePath = optarg;
                break;
            case 'h':
                printUsage(argv[0]);
                return EXIT_SUCCESS;
            default:
                printUsage(argv[0]);
                return EXIT_FAILURE;
        }
    }

    spdlog::info("Welcome to spdlog!");  // 输出INFO级别日志
    spdlog::error("Error: {}", 404);
     
    SettingsParser parser(filePath);

    // 示例：修改一些配置项
    parser.setSetting("device_num", "4");
    parser.setSetting("baudrate1", "9600");

    // 批量设置
    std::unordered_map<std::string, std::string> newSettings = {
        {"dll_path", "/home/root/new_dll_path"},
        {"modem1", "/dev/ttyUSB100"},
        {"device2", "/dev/ttyUSB200"}
    };
    parser.setSettings(newSettings);

    // 保存回文件
    if (parser.saveSettingsToFile()) {
        std::cout << "Settings saved successfully to: " << filePath << "\n";
    } else {
        std::cerr << "Failed to save settings.\n";
    }

    // 输出所有配置项（调试用）
    parser.printAllSettings();

    return 0;
}